﻿using System.Collections;
using System;
using UnityEngine;
using WssUtils;

namespace WssNative.Vibrations
{
    /// <summary>
    /// 游戏直接使用的 震动管理,可以读取数据 获取是否震动的设置
    /// </summary>
    public class VibrationController : SingletonMonoBehaviour<VibrationController>
    {


        public bool canVibrate = true;

        private void Awake()
        {
            MMVibrationManager.iOSInitializeHaptics();
            if (PlayerPrefs.HasKey("_ctrl_vibrate") == false)
            {
                PlayerPrefs.SetInt("_ctrl_vibrate", IsSupportLightVibrate() ? 1 : 0);
            }

            canVibrate = (PlayerPrefs.GetInt("_ctrl_vibrate", 1) == 1) ? true : false; //自动读取震动配置
        }

        public void Init()
        {
            //初始化配置
        }

        private bool IsSupportLightVibrate()
        {
            string modelStr = SystemInfo.deviceModel;
            if (modelStr.Contains("iPad"))
            {
                return false;
            }
            else if (modelStr.Contains("iPhone"))
            {
                var arr = modelStr.Split(',');
                var topStr = arr[0];
                string version = topStr.Replace("iPhone", "");
                int code = 0;
                int.TryParse(version, out code);
                if (code <= 8)
                {
                    return false;
                }

            }
            return true;
        }

        //销毁
        new private void OnDestroy()
        {
            base.OnDestroy();
            MMVibrationManager.iOSReleaseHaptics();
        }

        public void SetVibrate(bool enable)
        {
            canVibrate = enable;
            PlayerPrefs.SetInt("_ctrl_vibrate", (canVibrate == true) ? 1 : 0);
        }


        public void DelayImpact(float delay)
        {
            Invoke("Impact", delay);
        }

        /// <summary>
        /// 持续震动
        /// </summary>
        /// <param name="interval">震动频率</param>
        /// <param name="duration">震动总时间</param>
        public void ImpactSustain(float interval = 0.1f, float duration = 0.5f, float delay = 0f)
        {
            StartCoroutine(_ImpactSustain(interval, duration, delay));
        }

        private IEnumerator _ImpactSustain(float interval, float duration, float delay)
        {
            yield return new WaitForSeconds(delay);
            while (duration > 0)
            {
                yield return new WaitForSeconds(interval);
                Impact();
                duration -= interval;
            }
        }

        /// <summary>
        /// 轻度震动
        /// </summary>
        public void Impact()
        {
            if (canVibrate == false) { return; }
            if (!AudioManager.Instance.canPlayerShake)
            {
                return;
            }
            // print("impart");
            MMVibrationManager.Haptic(HapticTypes.LightImpact);
        }

        public void Haptic(HapticTypes type)
        {
            if (canVibrate == false) { return; }
            if (!AudioManager.Instance.canPlayerShake)
            {
                return;
            }
            MMVibrationManager.Haptic(type);
        }
    }
}
